package com.picturestory.client.android;

import java.io.FileInputStream;

import com.picturestory.client.android.auth.Auth;
import com.picturestory.client.android.registration.ServiceInfo;
import com.picturestory.client.android.util.Constants;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

public class Intro extends Activity {
    
	private static final String TAG = Intro.class.getSimpleName();
	
	private String mPin;
	private Intent lastIntent;
	
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			startActivity(lastIntent);
		}
		
	};
	
	private Handler mSignupHandler = new Handler() {
		@Override
		public void handleMessage(final Message msg) {
			lastIntent = new Intent(Intro.this, ServiceInfo.class);
			startActivity(lastIntent);
		}
	};

	private Handler mAuthHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			lastIntent = new Intent(Intro.this, Auth.class);
			startActivity(lastIntent);
		}
	};
	
	private Handler mServiceHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			lastIntent = new Intent(Intro.this, Dashboard.class);
			startActivity(lastIntent);
		}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Log.i(Constants.LOGTAG, " " + TAG + " is on create");
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        
        mPin = null;
        
        try {
			byte[] buffer = new byte[4096];
			FileInputStream fis = openFileInput(Constants.USER_FILE);
			fis.read(buffer, 0, 4096);
			mPin = new String(buffer);
			
			Log.i(Constants.LOGTAG, TAG + " : read pin from local");
		} catch (Exception e) {
			Log.i(Constants.LOGTAG, TAG + " : failed to open pin, move to signup");
			
			
			new Thread() {
				@Override
				public void run() {
					mSignupHandler.sendMessageDelayed(mSignupHandler.obtainMessage(), 3000);
				}
			}.start();
		}
		if (mPin != null) {
			Log.i(Constants.LOGTAG, TAG + " : autologin starts");
			Intent intent = new Intent(this, IntroHttpRequester.class);
			intent.putExtra("pin", mPin);
		
			startActivityForResult(intent, 1);
			
		}
    }
    
    

	/*@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		new Thread() {

			@Override
			public void run() {
				mHandler.sendMessageDelayed(mHandler.obtainMessage(), 2000);
			}
			
		}.start();
	}*/



	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		Log.i(Constants.LOGTAG, TAG + " : onActivityResult");
		
		if (resultCode == Constants.FAILED) {
			Toast.makeText(this, "Login failed, please authenticate yourself", Toast.LENGTH_LONG).show();
			new Thread() {
				@Override
				public void run() {
					mAuthHandler.handleMessage(mAuthHandler.obtainMessage());
				}
			}.start();
		} else {
		
			Log.i(Constants.LOGTAG, TAG + " : successfully obtained a new pin");
		//	write this pin to local
			mServiceHandler.handleMessage(mServiceHandler.obtainMessage());
		}
	}
    
	
    
}